﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using nSL.Config;
using System.Data;
using nSL.Libs;
using System.IO;
using System.Threading;
using System.Windows.Threading;
using System.Net;

namespace nSL.UC
{
    /// <summary>
    /// Interaction logic for Splash.xaml
    /// </summary>
    public partial class Splash : UserControl
    {
        public Splash()
        {
            InitializeComponent();
            this.LayoutUpdated += new EventHandler(Splash_LayoutUpdated);
            i = 1;
        }
        Thread th;
        void Splash_LayoutUpdated(object sender, EventArgs e)
        {
            if (i++ == 30)
            {
                th = new Thread(new ThreadStart(WaitAndGo));
                th.SetApartmentState(ApartmentState.STA);
                th.Start();
            }
                //throw new NotImplementedException();
        }
        int i = 1;
        private Common.LECTURE_INFO _lectureInfo;
        public Common.LECTURE_INFO LectureInfo
        {
            get { return _lectureInfo; }
            set { _lectureInfo = value; }
        }

        private void BatchDownload()
        {
            string lectureID = LectureInfo.lecture_id;
            DataTable dtLectureFile = Common.COURSE_FILE.Tables[0];
            DataRow[] rows = dtLectureFile.Select("LectureID=" + lectureID);

            foreach (DataRow dr in rows)
            {
                Common.LECTURE_FILE_INFO fileInfo = new Common.LECTURE_FILE_INFO();
                fileInfo.lecture_id = dr["LectureID"].ToString();
                fileInfo.file_id = dr["Vdoid"].ToString();
                fileInfo.file_desp = dr["VdoName"].ToString();
                fileInfo.file_type = dr["TypeFile"].ToString();
                
                string imgURL = "http://";
                imgURL += Config.Configurations.Server;
                imgURL += "/eThumbs/";
                imgURL += LectureInfo.course_id;
                imgURL += "/";
                imgURL += fileInfo.file_desp + ".jpg";

                //download all thumbnails
                string _chkPath = Common.TEMP_PATH + EncryptHelper.CalculateSHA1(imgURL) + ".jpg";
                if (!File.Exists(_chkPath))
                {
                    FileHelper.Downloader(imgURL, _chkPath);
                }
            }
        }

        private void WindowTrans()
        {
            UC.Lecture lecture = new Lecture();
            lecture.LectureInfo = LectureInfo;
            Common.lecture_info = LectureInfo;
            WpfPageTransitions.PageTransition transition = new WpfPageTransitions.PageTransition();
            transition.TransitionType = WpfPageTransitions.PageTransitionType.Slide;
            transition.ShowPage(lecture);
            Window.GetWindow(this).Content = transition;
        }

        private void WaitAndGo()
        {
            BatchDownload();

            //textBlockState
            this.Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
            WindowTrans()
            ));

            if (th.IsAlive)
            {
                th.Abort();
            }
        }
    }
}
